import axios from 'axios'

// 获取API基础URL
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || '/api'

/**
 * 发送AI请求
 * @param {string} model - AI模型标识 'tongyi' | 'dify'
 * @param {Array} messages - 对话历史
 * @returns {Promise<string>} AI回复内容
 */
export const sendAIRequest = async (model, messages) => {
    try {
        const response = await axios.post(`${API_BASE_URL}/ai/chat`, {
            model,
            messages
        }, {
            timeout: 60000 // 60秒超时
        })

        return response.data.content
    } catch (error) {
        console.error('AI请求失败:', error)
        throw new Error(error.response?.data?.message || '请求AI服务失败，请稍后再试')
    }
}

/**
 * 获取AI模型列表
 * @returns {Promise<Array>} 模型列表
 */
export const getAvailableModels = async () => {
    try {
        const response = await axios.get(`${API_BASE_URL}/ai/models`)
        return response.data
    } catch (error) {
        console.error('获取模型列表失败:', error)
        return [
            { id: 'tongyi', name: '通义千问', available: true },
            { id: 'dify', name: 'Dify本地模型', available: true },
            { id: 'yuanbao', name: '腾讯元宝', icon: 'yuanbao-icon' }
        ]
    }
}